﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace StudentManagement
{
    public class Startup
    {
        private IConfiguration _configuration;

        public Startup(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        // 添加服务
        public void ConfigureServices(IServiceCollection services)
        {
            //包含了依赖于MvcCore以及相关的第三方常用的服务和方法
            services.AddMvc().AddXmlSerializerFormatters();//支持xml序列化
            //只包含核心的Mvc功能
            //services.AddMvcCore();
            #region Ioc之依赖注入
            //services.AddSingleton();//应用程序生命周期中共用一个实例
            //services.AddScoped();//一次请求生命周期中共用一个实例
            //services.AddTransient();//每次使用时创建新的实例
            #endregion
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        // 添加中间件
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILogger<Startup> logger)
        {
            #region 环境变量
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                if (env.IsStaging() || env.IsProduction() || env.IsEnvironment("UAT"))
                {
                    app.UseExceptionHandler("/Error");
                }
            }
            #endregion

            #region 文件访问中间件
            //添加默认文件中间件
            //默认文件：default.html default.htm index.html index.htm
            app.UseDefaultFiles();

            //添加静态文件中间件
            app.UseStaticFiles();

            //添加文件夹浏览中间件（不推荐使用，正常生产环境不会直接浏览文件夹）
            //app.UseDirectoryBrowser();

            //包含三个中间件服务：UseDefaultFiles、UseStaticFiles、UseDirectoryBrowser（不推荐使用）
            //app.UseFileServer();
            #endregion

            #region Mvc中间件
            app.UseMvcWithDefaultRoute();
            #endregion

            //app.Use(async (context, next) =>
            //{
            //    context.Response.ContentType = "text/plain;charset=utf-8";
            //    await context.Response.WriteAsync("第一个中间件开始运行\r\n");
            //    await next();
            //});

            //app.Run(async (context) =>
            //{
            //    await context.Response.WriteAsync("Hello World");
            //});
        }
    }
}
